<template>
  <div class="home">
    <div class="home-top">
      <span class="left-part">数字迎新服务平台|自助报到</span>
      <span class="right-part">
        <router-link to="/">首页</router-link>
      </span>
    </div>
    <div class="home-body">
      <div class="choice">
        <!-- 绿色通道申办 -->
        <router-link to="/GreenChannel" class="ye">
          <el-icon><Guide /></el-icon>
          <p>绿色通道申办</p>
        </router-link>
        <!-- 住宿分配信息 -->
        <el-button @click="openDebounced" class="ye">
          <div style="flex: 1;">
            <el-icon style="color: #ffffff; font-size: 7em;"><Avatar /></el-icon>
            <p>住宿分配信息</p>
          </div>
        </el-button>
        <!-- 缴费情况 -->
        <el-button @click="getPaymentStatus" class="ye">
          <div style="flex: 1;">
            <el-icon style="color: #ffffff; font-size: 7em;"><CreditCard /></el-icon>
            <p>财务缴费</p>
          </div>
        </el-button>

        <el-dialog
          v-model="centerDialogVisible"
          title="缴费状态"
          width="30%"
          align-center
        >
          <span>当前缴费状态：{{ paymentStatus }}</span>
          <template #footer>
            <span class="dialog-footer">
              <el-button type="primary" @click="jump">
                线上缴费
              </el-button>
              <el-button type="primary" @click="centerDialogVisible = false">
                确认
              </el-button>
            </span>
          </template>
        </el-dialog>
        <!-- 心理健康测试 -->
        <router-link to="/MentalTest" class="ye">
          <el-icon><Document /></el-icon>
          <p>心理健康测试</p>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { ElMessage, ElMessageBox } from 'element-plus';
import _ from 'lodash';
import { sroom,payment } from '@/api/user';
import {getId,getRole} from '../../utils/jwt-decode'

export default {
  data() {
    return {
      centerDialogVisible: false,
      paymentStatus: '',
      id: '',
    };
  },
  methods: {
    jump(){
      this.$router.push('/OnlinePay');
    },
    openDebounced: _.debounce(function() {
      this.open();
    }, 3000),

    async open() {
      try {
        // 获取当前用户id
        const role= getRole();
        console.log(role)
        console.log(this.id)
        // 获取学生宿舍信息
        const resp = await sroom({id:this.id });
        const code = resp.data.code;
        if (code === 200) {
          const accommodationInfo = resp.data.data['0'];
          const content = `
            <div>
              <p>姓名：${accommodationInfo.SNAME}</p>
              <p>学号：${accommodationInfo.SNO}</p>
              <p>性别：${accommodationInfo.SSEX}</p>
              <p>寝室：${accommodationInfo.DORM_ID}</p>
            </div>
          `;

          ElMessageBox.alert(content, '学生住宿信息', {
            confirmButtonText: '确定',
            callback: () => {
              ElMessage({
                type: 'success',
                message: '欢迎',
              });
            },
            dangerouslyUseHTMLString: true,
          });
        } else {
          throw new Error('获取学生住宿信息失败');
        }
      } catch (error) {
        console.error(error);
        ElMessage({
          type: 'error',
          message: '获取学生住宿信息失败',
        });
      }
    },
    async getPaymentStatus() {
      this.centerDialogVisible = true
      try {
        const res = await payment({id:this.id});
        if (res.data.code === 200) {
          this.paymentStatus = res.data.data; 
          this.centerDialogVisible = true;
        } else {
          throw new Error('获取缴费状态失败');
        }
      } catch (error) {
        console.error(error);
      }
    },
  },
  created() { 
    this.id= getId();
  },
}
</script>

<style scoped>
  .home{
    min-height: 100vh;
    min-width: 100vw;
    background: rgba(242, 241, 241, 1);
  }
  .left-part{
    position: relative;
    left: 30px;
    font-family: "华文行楷", sans-serif;
  }
  .right-part{
    position: relative;
    right: 60px;
    border-width: 0px;
  }
  .home-top{
    height: 80px;
    background-color: rgba(26, 188, 156, 1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 28px;
    color: rgb(255, 255, 255);
  }
  .choice{
    display: flex;
    justify-content: center;
    align-items: flex-start;
    flex-wrap: wrap;
    position: relative;
  }
  /* 点击时去掉下划线 */
  .router-link-active {     
    text-decoration: none;
  }
/* 去掉原有链接文字下划线 */
  a {
    text-decoration: none; 
    font-size: 28px;
    color: #ffffff;
  }
  .ye{
    background-color: rgba(26, 188, 156, 1);
    width: 200px;
    height: 170px;
    margin: 40px;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    /* justify-content: flex-end; */
    align-items: center;
  }
  .el-icon{
    font-size: 3em;
  }
  p {
    color: #ffffff;
    margin: 5px;
    font-size: 20px;
  }

  .dialog-footer button:first-child {
  margin-right: 10px;
}
</style>